package com.liuchuanzheng.lcz_study_demo.behavior;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

import com.liuchuanzheng.lcz_study_demo.util.log.LogUtil;

import org.jetbrains.annotations.NotNull;

public class BottomShowBehavior extends CoordinatorLayout.Behavior<TextView>{

    public BottomShowBehavior(Context context, AttributeSet attrs) {
        super(context,attrs);
    }

    // 这个方法的回调时机 ： 即将发生嵌套滚动时 Axes用于判断滑动的方向
    //返回true,之后的回调事件才会触发到,表示我想处理滑动,至于处理多少,如何处理,是后边回调的事情
    @Override
    public boolean onStartNestedScroll(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View directTargetChild, @NonNull @NotNull View target, int axes, int type) {
        LogUtil.i("onStartNestedScroll");
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    //时机:子view(实现了NestedScrollingChild接口)的view将要滑动的时,触发无数次
    @Override
    public void onNestedPreScroll(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View target, int dx, int dy, @NonNull @NotNull int[] consumed, int type) {
        LogUtil.i("onNestedPreScroll");
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }

    //时机:onNestedPreScroll处理后,子view消费滑动后,又回到这.触发无数次
    @Override
    public void onNestedScroll(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull @NotNull int[] consumed) {
        LogUtil.i("onNestedScroll");
        //手指向上滑动
        if (dyConsumed + dyUnconsumed > 0) {
            //隐藏child
            if (child.getVisibility() == View.VISIBLE) {
                Anim.bottomHide(child);
            }
            //手指向下滑动
        } else {
            //展示child
            if (child.getVisibility() != View.VISIBLE) {
                Anim.bottomShow(child);
            }
        }
    }

    //时机:子view将要惯性滑动时
    @Override
    public boolean onNestedPreFling(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View target, float velocityX, float velocityY) {
        LogUtil.i("onNestedPreFling");
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }

    //时机:onNestedPreFling处理后,子view又处理了惯性滑动,然后又回到这
    @Override
    public boolean onNestedFling(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View target, float velocityX, float velocityY, boolean consumed) {
        LogUtil.i("onNestedFling");
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    //时机:停止嵌套滑动
    @Override
    public void onStopNestedScroll(@NonNull @NotNull CoordinatorLayout coordinatorLayout, @NonNull @NotNull TextView child, @NonNull @NotNull View target, int type) {
        LogUtil.i("onStopNestedScroll");
        super.onStopNestedScroll(coordinatorLayout, child, target, type);
    }

    //是否拦截触摸事件
    @Override
    public boolean onInterceptTouchEvent(@NonNull @NotNull CoordinatorLayout parent, @NonNull @NotNull TextView child, @NonNull @NotNull MotionEvent ev) {
        LogUtil.i("onInterceptTouchEvent");
        return super.onInterceptTouchEvent(parent, child, ev);
    }

    //处理触摸事件
    @Override
    public boolean onTouchEvent(@NonNull @NotNull CoordinatorLayout parent, @NonNull @NotNull TextView child, @NonNull @NotNull MotionEvent ev) {
        LogUtil.i("onTouchEvent");
        return super.onTouchEvent(parent, child, ev);
    }

    //确定使用Behavior的View要依赖的View的类型
    @Override
    public boolean layoutDependsOn(@NonNull @NotNull CoordinatorLayout parent, @NonNull @NotNull TextView child, @NonNull @NotNull View dependency) {
        LogUtil.i("layoutDependsOn");
        return super.layoutDependsOn(parent, child, dependency);
    }

    //当被依赖的View移除时回调
    @Override
    public void onDependentViewRemoved(@NonNull @NotNull CoordinatorLayout parent, @NonNull @NotNull TextView child, @NonNull @NotNull View dependency) {
        LogUtil.i("onDependentViewRemoved");
        super.onDependentViewRemoved(parent, child, dependency);
    }

    public static class Anim {

        public static void bottomShow(View show) {
            //展示的动画
            show.clearAnimation();
            TranslateAnimation animationShow = new TranslateAnimation(1F,1f,1f,0f);
            animationShow.setDuration(2000);
            show.startAnimation(animationShow);
            show.setVisibility(View.VISIBLE);
        }

        public static void bottomHide(View gone) {
            //消失的动画
            gone.clearAnimation(); //清楚自身动画
            Animation animationGone = new TranslateAnimation(1F,1f,0f,1f);
            animationGone.setDuration(2000);
            gone.startAnimation(animationGone);
            gone.setVisibility(View.INVISIBLE);
        }

    }
}